#include<bits/stdc++.h>
using namespace std;
#pragma warning(disable:4996)
struct ppp {
	int a;
	int b;
};

struct node {
	int id;
	int val;
	int priority;
	node *left = nullptr, *right = nullptr;
};

ppp arr[32];
int n;
int in[32];
int hp[32];

node *root = nullptr;

void create(node *&r, int hl, int hr) {
	if (hl > hr) return;

	int minindex = -1;
	for (int i = hl; i <= hr; i++) {
		if (minindex == -1) minindex = i;
		else {
			if (hp[i] < hp[minindex]) {
				minindex = i;
			}
		}
	}

	r = new node;
	r->val = in[minindex];
	r->priority = hp[minindex];
	create(r->left, hl, minindex - 1);
	create(r->right, minindex + 1, hr);


}

bool cmp(ppp &x, ppp &y) {
	return x.a < y.a;
}

int main() {

	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		int a, b;
		scanf("%d %d", &a, &b);
		arr[i].a = a;
		arr[i].b = b;
	}
	sort(arr, arr + n, cmp);
	for (int i = 0; i < n; i++) {
		in[i] = arr[i].a;
		hp[i] = arr[i].b;
	}

	create(root, 0, n-1);
	//cout << root->val << " ";
	int ans[32];
	int pans[32];
	int cnt = 0;
	queue<node*> q;
	q.push(root);
	while (!q.empty()) {
		node *temp = q.front();
		q.pop();
		ans[cnt] = temp->val;
		pans[cnt] = temp->priority;
		cnt++;
		if (temp->left != nullptr) {
			q.push(temp->left);
		}

		if (temp->right != nullptr) {
			q.push(temp->right);
		}
	}
	for (int i = 0; i < n; i++) {
		if (i != 0) printf(" ");
		printf("%d",ans[i]);
	}
	printf("\n");
	for (int i = 0; i < n; i++) {
		if (i != 0) printf(" ");
		printf("%d", pans[i]);
	}



	system("pause");
	return 0;
}